package scau.erps.view.base;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import scau.erps.app.Erps;
import scau.erps.entity.Sand;
import scau.erps.entity.Team;
import scau.erps.service.TeamService;
import scau.erps.service.impl.TeamServiceImpl;
import scau.erps.view.LoginDialog;
import scau.erps.view.MainFrame;

/**
 * 修改密码
 * @author ZJC
 *
 */

public class ModifyDialog extends JDialog{
	
private JLabel label_BeforeModify,label_AfterModify_1,label_AfterModify_2,team;
private JPasswordField jtf_BeforeModify,jtf_AfterModify_1,jtf_AfterModify_2;
private JButton confirmButton,cancelButton;
private JPanel panel;
private JComboBox<String> teamNameField;

	public ModifyDialog() {
		this.setTitle("修改密码");
		this.setSize(340, 340);
		panel=new JPanel();
		panel.setLayout(null);
		team = new JLabel("小    组");
		team.setBounds(30, 20, 80, 30);
		panel.add(team);
		teamNameField = new JComboBox<String>(new String[]{"选择小组","第1组","第2组","第3组","第4组","第5组","第6组","第7组","第8组"});
		teamNameField.setBounds(110, 20, 140, 30);
		panel.add(teamNameField);
		label_BeforeModify = new JLabel("原密码");
	    label_BeforeModify.setBounds(30, 70, 80, 30);
		panel.add(label_BeforeModify);
		jtf_BeforeModify=new JPasswordField();
		jtf_BeforeModify.setBounds(110, 70, 140, 30);
		panel.add(jtf_BeforeModify);
		label_AfterModify_1 = new JLabel("输入新密码");
		label_AfterModify_1.setBounds(30, 120, 80, 30);
		panel.add(label_AfterModify_1);
		jtf_AfterModify_1 = new JPasswordField();
		jtf_AfterModify_1.setBounds(110, 120, 140, 30);
		panel.add(jtf_AfterModify_1);
		label_AfterModify_2 = new JLabel("确认新密码");
		label_AfterModify_2.setBounds(30, 170, 80, 30);
		panel.add(label_AfterModify_2);
		jtf_AfterModify_2 = new JPasswordField();
		jtf_AfterModify_2.setBounds(110, 170, 140, 30);
		panel.add(jtf_AfterModify_2);
		confirmButton = new JButton("确    定");
		confirmButton.setBounds(30, 220, 100, 30);
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TeamService teamService = null;
				try {
					teamService = Erps.getIocInstance().get(TeamServiceImpl.class);
					int teamId =teamNameField.getSelectedIndex();
					String password = String.valueOf(jtf_BeforeModify.getPassword());
					String newPassword_1=String.valueOf(jtf_AfterModify_1.getPassword());
					String newPassword_2=String.valueOf(jtf_AfterModify_2.getPassword());
					if(teamId==0||password.equals("")||newPassword_1.equals("")||newPassword_2.equals("")){
						JOptionPane.showMessageDialog(null, "请输入必要信息", "提示",JOptionPane.INFORMATION_MESSAGE);
					} else {
						if (teamService.loginCheck(teamId,password)==false)
						{
						JOptionPane.showMessageDialog(null, "认证失败！请输入正确原密码", "提示",JOptionPane.ERROR_MESSAGE);
						}
						else
						 {
							if(newPassword_1.equals(newPassword_2)){
								if(teamService.modifyPassword(teamService.getTeamById(teamId),newPassword_1)){
								JOptionPane.showMessageDialog(null, "修改密码成功", "提示",JOptionPane.INFORMATION_MESSAGE);
								dispose();
								new LoginDialog();
								}
								else{
								JOptionPane.showMessageDialog(null, "修改密码失败", "提示",JOptionPane.ERROR_MESSAGE);
								}
							
						} else{
							JOptionPane.showMessageDialog(null, "两次新密码不相同，请再次确认", "提示",JOptionPane.ERROR_MESSAGE);
						}
						}
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		panel.add(confirmButton);
		
		cancelButton = new JButton("取消");
		cancelButton.setBounds(150, 220, 100, 30);
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
				new LoginDialog();
			}
		});			
		panel.add(cancelButton);
		
		getContentPane().add(panel, BorderLayout.CENTER);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
}
